//app.js
var config = require('config.js');
var util = require('network_util.js');
App({
  onLaunch: function () {
   
    //获取屏幕信息
    // console.log(wx.getSystemInfoSync());
    wx.setStorageSync("systemInfo", wx.getSystemInfoSync());
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // this.globalData.deviceInfo = wx.getSystemInfoSync();
    // console.log(this.globalData.deviceInfo);
    var that = this;
    wx.login({
      success: function (res) {
        // console.log("wx.login.result: ");
        // console.log(res);
        //获取用户微信身份信息
        wx.getUserInfo({
          success: function(res){
            that.globalData.wxUserInfo = res.userInfo;
            wx.setStorageSync("wxUserInfo", res.userInfo);
          }
        })
        //获取OpenID
        if (res.code) {
          // console.log("code ok");
          //发起网络请求
          wx.request({
            url: config.getOpenId,
            data: {
              code: res.code
            },
            success: function (res) {
              // console.log("getopendi success");
              // console.log(res);
              if (res.statusCode == 200) {
                if (res.data.code == 200) {//成功
                  // that.setData({ payDetail: res.data.data[0] });
                  // that.globalData.openId = res.data.data.openId;
                  // console.log( res.data.data.openid);
                  wx.setStorage({ "key": "openId", data: res.data.data.openid });
                  wx.getStorage({
                    key: 'openId',
                    success: function (res) {
                      // console.log("wx.setstorage.getstorage = ");
                      // console.log(res);
                    }
                  })
                } else {//接口错误
                  wx.showToast({
                    title: '获取openId出错(' + res.data.code + ')',
                  })
                }
              } else {//系统错误
                wx.showToast({
                  title: '获取openId发生系统错误(' + res.statusCode + ')',
                })
              }

            },
            fail: function (res) {
             
            },
            complete: function (res) {
              // console.log("getopendi complete");
            }
          })
        } else {
          // console.log('获取用户登录态失败！' + res.errMsg)
        }
      },
      fail: function (fr) {
        // console.log("wx.login.fail:");
        // console.log(fr);
      },
      complete: function (cr) {
        // console.log("wx.login.complete:");
        // console.log(cr);
      }
    });

    //如果用户已登录，判断是否有未支付订单
    var userInfo = wx.getStorageSync("userInfo");
    // console.log(userInfo);
    if (userInfo != null && userInfo != "") {
      util._get(config.homeAdvertising + "?userId=" + userInfo.id
        , function (res) {
          // console.log("homeAdvertising=");
          // console.log(res);
          if (res.statusCode == 200) {
            if (res.data.code == 200) {//成功
              // that.setData({ payDetail: res.data.data[0] });
              if (res.data.data.items && res.data.data.items.orderId != "") {
                wx.showModal({
                  title: '您有行程尚未完成支付',
                  content: '未支付将影响到您再次使用专车服务，请尽快完成支付',
                  confirmText: "立即支付",
                  cancelText: "稍后再说",
                  success: function (res2) {
                    if(res2.confirm){
                      console.log("tap confirm");
                      wx.navigateTo({
                        url: '/pages/placing/order?orderId=' + res.data.data.items.orderId,
                      })
                    }
                  },
                })
              } else if (res.data.data.unComplax && res.data.data.unComplax.length > 0){
                wx.showModal({
                  title: '您有一个进行中的订单',
                  // content: '未支付将影响到您再次使用专车服务，请尽快完成支付',
                  confirmText: "查看订单",
                  cancelText: "我知道了",
                  success: function (res2) {
                    if (res2.confirm) {
                      console.log("tap confirm");
                      wx.navigateTo({
                        url: '/pages/placing/order?orderId=' + res.data.data.unComplax[0].orderId,
                      })
                    }
                  }
                })
              } else {
                console.log("没有未支付和未完成的订单");
              }
            } else {//接口错误
              wx.showToast({
                title: '首页轮播接口出错(' + res.data.code + ')',
              })
            }
          } else {//系统错误
            wx.showToast({
              title: '首页轮播接口发生系统错误(' + res.statusCode + ')',
            })
          }
        })
    }
  },

  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function (res) {
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    wxUserInfo: { avatarUrl: "../../images/avatar-d.png"},
    // showMenuOrNot,
    deviceInfo: null,
    orderStatus: ["所有订单", "等待接单", "预约成功",
      "预约成功", "预约成功", "已发车",
      "已到达上车地点", "乘客上车开始行程", "行程中", "待付款",
      "已完成(未评价)", "已完成(已评价)", "已取消(不收费)",
      "待付款(已取消)", "已取消(已支付)", "已结算", "行程中(待结算)"],
    // orderStatus: ["所有订单", "等待接单", "预约成功(未确认出行管家)",
    //   "预约成功(确认出行管家)", "预约成功(确认司机)", "已发车",
    //   "已到达上车地点", "乘客上车开始行程", "行程中", "待付款",
    //   "已完成(未评价)", "已完成(已评价)", "已取消(不收费)",
    //   "待付款(已取消)", "已取消(已支付)", "已结算"],
    tripType: ["所有类型", "立即接机", "预约接机"],
    openId: "",
    latestFromInfo:""
  }
})
